//*****************************************************************************
//
//									CPU.c
//
//*****************************************************************************
//
//
#include "config.h"
#include "typedefs.h"
#include "reg.h"
#include "printf.h"
#include "I2C.h"
#include "CPU.h"
#include "Global.h"
#include "KeyRemo.h"
#include "TouchScreen.h"

DATA 	WORD   	tm01=0;
DATA 	WORD    tic01=0;
DATA    WORD    tic_pc; //erek 2011-11-10

DATA 	DWORD	SystemClock=0L;

XDATA 	BYTE	RS_buf[RS_BUF_MAX];
DATA 	BYTE	RS_in, RS_out;
		bit		RS_Xbusy=0;
#ifdef DP80390
XDATA 	BYTE	RS1_buf[RS_BUF_MAX];
DATA 	BYTE	RS1_in, RS1_out;
		bit		RS1_Xbusy=0;
#endif
		bit		F_watch=0;

DATA	BYTE	XDATA *DATA regTW88 = REG_START_ADDRESS;

DATA	BYTE	EXINT_STATUS = 0;
XDATA   BYTE    touch_flag = 0;

#define MAXTIMER0_TASK 4
BYTE Timer0TaskAct = 0;
BYTE Timer0TaskDly[MAXTIMER0_TASK] = {0,0,0,0};

#ifdef TW8823SHDEMO
bit 	D115_ON = 0;
#endif

//*****************************************************************************
//*      Ext Int 0 Interrupt (Low / Falling)   : remocon
//*****************************************************************************
void	ext0_int(void) interrupt 0 using 1
{
	PageREG = ReadTW88( 0xff );
	//IntIR();
	WriteTW88( 0xff, PageREG );
	EX0 = 1;	// Enable Remocon (Enable Ext int0)
}
//*****************************************************************************
//*      Timer 0 Interrupt                                                
//*****************************************************************************
//      Timer 0 Interrupt                                                  
//			If TL0 overflow,
//			 .Invoke this interrupt
//			 .TL0 <- TH0
//			TL0 is incremented every machine cycle
//			Every machine cycle is 12*Tosc(11.0592MHz)
//
//			Every machine cycle = 1.085us
//			Interrupt interval = 208us ( 1.085*(256-64(TH0)) )
//			When tm001==48, it's 0.01sec.  48*208us
//							
//*****************************************************************************
void timer0_int(void) interrupt 1 using 1
{
//	SwapLED113;
	//tic01 %= 30000;
	tic_pc++;   //erek 2011-11-10
	tic01++;
	
	tic01%=1000; //erek 2011-11-10
	if(tic01 == 0) {				// 1 sec
		//SwapLED113;
		SystemClock++;
	}

	if(tic01%10 == 0){//10ms
		BYTE i=0;
		for(i=0; i<MAXTIMER0_TASK; i++){
			if(Timer0TaskDly[i] != 0){
				Timer0TaskDly[i]--;
			}
		}
	}
}
//*****************************************************************************
//*      Ext Int 1 Interrupt  (Low / Falling)		  : internal IRQ
//*****************************************************************************
void ext1_int(void) interrupt 2 using 1
{
	P1_7 = 1;
	EX1 = 1;
	P1_7 = 0;
}
//****************************************************************************
//*      Timer 1 Interrupt                                                
//****************************************************************************
void timer1_int(void) interrupt 3 using 1
{

}
//*****************************************************************************
//      UART 0 Interrupt                                                   
//*****************************************************************************
#ifdef DP80390
void uart0_int(void) interrupt 4 using 1
{
BYTE	count;
EA = 0;
#ifdef DEBUG_ISR
	MCU_INT_STATUS |= 0x0010;
#endif
	if( RI ) {					//--- Receive interrupt ----
		RI = 0;
		if ( UART0FIFO & 0x80 ) {			// use fifo?
			count = UART0FIFO & 0x1F;
			if ( count & 0x10) {
				UART0FIFO = 0x90;		// overflowed, buffer clear
			}
			else {
				while (count) {
					RS_buf[RS_in++] = SBUF;
					if( RS_in>=RS_BUF_MAX ) RS_in = 0;
					count--;
				};
			}
		}
		else {
			RS_buf[RS_in++] = SBUF;
			if( RS_in>=RS_BUF_MAX ) RS_in = 0;
		}
	}

	if( TI ) {					//--- Transmit interrupt ----
		TI = 0;
		RS_Xbusy=0;
	}
EA = 1;
}
#else
void uart0_int(void) interrupt 4 using 1
{
	if( RI ) {					//--- Receive interrupt ----
		RI = 0;
		RS_buf[RS_in++] = SBUF;
		if( RS_in>=RS_BUF_MAX ) RS_in = 0;
	}

	if( TI ) {					//--- Transmit interrupt ----
		TI = 0;
		RS_Xbusy=0;
	}
}
#endif
//****************************************************************************
//*      Timer 2 Interrupt                                                
//****************************************************************************
void timer2_int(void) interrupt 5 using 1
{
	TF2 = 0;					// clear overflow

	tm01++;

}
//*****************************************************************************
//      UART 1 Interrupt                                                   
//*****************************************************************************
#ifdef DP80390
void uart1_int(void) interrupt 6 using 1
{
BYTE	count;

EA = 0;
#ifdef DEBUG_ISR
	MCU_INT_STATUS |= 0x0040;
#endif
	if( RI1 ) {					//--- Receive interrupt ----
		RI1 = 0;
		if ( UART1FIFO & 0x80 ) {			// use fifo?
			count = UART1FIFO & 0x1F;
			if ( count & 0x10) {
				UART1FIFO = 0x90;		// overflowed, buffer clear
			}
			else {
				while (count) {
					RS1_buf[RS1_in++] = SBUF1;
					if( RS1_in>=RS_BUF_MAX ) RS1_in = 0;
					count--;
				};
			}
		}
		else {
			RS1_buf[RS1_in++] = SBUF1;
			if( RS1_in>=RS_BUF_MAX ) RS1_in = 0;
		}
	}

	if( TI1 ) {					//--- Transmit interrupt ----
		TI1 = 0;
		RS1_Xbusy=0;
	}
EA = 1;
}
#else
void uart1_int(void) interrupt 6 using 1
{
	if( RI1 ) {					//--- Receive interrupt ----
		RI1 = 0;
		//RS1_buf[RS1_in++] = SBUF1;
		//if( RS1_in>=RS_BUF_MAX ) RS1_in = 0;
	}

	if( TI1 ) {					//--- Transmit interrupt ----
		TI1 = 0;
		//RS1_Xbusy=0;
	}
}
#endif
//*****************************************************************************
//*      Ext Int 2 Interrupt  (Low)			  : Touch Panel Ready		 EXIF0	P2_2
//*****************************************************************************
void ext2_int(void) interrupt 7 using 1
{
//	EXIF = EXIF & 0xFE;		// clear interrupt 2;
}
//*****************************************************************************
//*      Ext Int 3 Interrupt (Low), internal Compare0 : connect to OSD interrupt  EXIF1
//*****************************************************************************
void ext3_int(void) interrupt 8 using 1
{
	return;

	EA = 0;

	EXIF = EXIF & 0xFD;		// clear interrupt 3; no affect
	PageREG = ReadTW88( 0xff );
	WriteTW88( 0xff, OSD_PAGE );
	WriteTW88( 0xf0, 0x30 );	// clear DISPLAY and OSD
	WriteTW88( 0xff, PageREG );

	EA = 1;
}
//*****************************************************************************
//*      Ext Int 4 Interrupt (Low), internal Compare1						  EXIF2
//*****************************************************************************
void ext4_int(void) interrupt 9 using 1
{
	P1_4 = 1;
	EXIF = EXIF & 0xFB;		// clear interrupt 4;
	P1_4 = 0;
}
//*****************************************************************************
//*      Ext Int 5 Interrupt (Falling), internal Compare2					  EXIF3
//*****************************************************************************
void ext5_int(void) interrupt 10 using 1
{
	P1_5 = 1;
	EXIF = EXIF & 0xF7;		// clear interrupt 5;
	P1_5 = 0;
}
//*****************************************************************************
//*      Ext Int 6 Interrupt (Falling), internal Compare3
//*****************************************************************************
void ext6_int(void) interrupt 11 using 1
{
	P1_6 = 1;
	EXIF = EXIF & 0xEF;		// clear interrupt 6;
	P1_6 = 0;
}

//*****************************************************************************
//*      Watchdog Interrupt
//*****************************************************************************
void watchdog_int(void) interrupt 12 using 0
{
	EWDI = 0;

	F_watch = 1;
/*
	TA = 0xaa;
	TA = 0x55;
	WDCON = 0x03;	// - - - - WDIF WTRF EWT RWT
*/
	EWDI = 1;
}

DWORD IntCount = 0;

//*****************************************************************************
//*      Ext Int 7 Interrupt (Programable)
//*****************************************************************************
void ext7_int(void) interrupt 13 using 1
{
	EXINT_STATUS |= 1;
	IntCount++;
}
//*****************************************************************************
//*      Ext Int 8 Interrupt (Programable)
//*****************************************************************************
void ext8_int(void) interrupt 14 using 1
{
	EXINT_STATUS |= 2;
}

//*****************************************************************************
//*      Ext Int 9 Interrupt (Programable)
//*****************************************************************************
void ext9_int(void) interrupt 15 using 1
{
	EXINT_STATUS |= 4;

	touch_flag = 1;
	INTFLAG = INTFLAG & 0xFB;		// clear interrupt 9;
}
//*****************************************************************************
//*      Ext Int 10 Interrupt (Programable)
//*****************************************************************************
void ext10_int(void) interrupt 16 using 1
{
	EXINT_STATUS |= 8;
}
//*****************************************************************************
//*      Ext Int 11 Interrupt (Programable)
//*****************************************************************************
void ext11_int(void) interrupt 17 using 1
{
	EXINT_STATUS |= 16;
}
//*****************************************************************************
//*      Ext Int 12 Interrupt (Programable)
//*****************************************************************************
void ext12_int(void) interrupt 18 using 1
{
	EXINT_STATUS |= 32;
}
//*****************************************************************************
//*      Ext Int 13 Interrupt (Programable)
//*****************************************************************************
void ext13_int(void) interrupt 19 using 1
{
	EXINT_STATUS |= 64;
}
//*****************************************************************************
//*      Ext Int 14 Interrupt (Programable)
//*****************************************************************************
void ext14_int(void) interrupt 20 using 1
{
	EXINT_STATUS |= 128;
}
//*****************************************************************************


//=============================================================================
//		Serial RX Check 												   
//=============================================================================
BYTE RS_ready(void)
{
	if( RS_in == RS_out ) return 0;
	else return 1;
}
//=============================================================================
//		Serial RX														   
//=============================================================================
BYTE RS_rx(void)
{
	BYTE	ret;
		
	ES = 0;
	ret = RS_buf[RS_out];
	RS_out++;
	if(RS_out >= RS_BUF_MAX) 
		RS_out = 0;
	ES = 1;

	return ret;
}
void RS_ungetch(BYTE ch)
{
	RS_buf[RS_in++] = ch;
	if( RS_in>=RS_BUF_MAX ) RS_in = 0;
}

BYTE read_uart_test(void) //erek 2011-11-7
{
	BYTE  ch;
	
	if( !RS_ready() )
	return 0;

	ch = RS_rx();

	return  ch;
}



//=============================================================================
//		Serial TX														   
//=============================================================================
void RS_tx(BYTE tx_buf)
{
	while(RS_Xbusy);

	ES = 0;			// To protect interrupt between SBUF and RS_Xbusy when it's slow
	SBUF = tx_buf;
	RS_Xbusy=1;
	ES = 1;
}

#if 0
XDATA 	BYTE	RS1_buf[RS_BUF_MAX];
DATA 	BYTE	RS1_in, RS1_out;
		bit		RS1_Xbusy=0;
		
BYTE RS1_ready(void);
BYTE RS1_rx(void);
void RS1_ungetch(BYTE ch);
void RS1_tx(BYTE tx_buf);
//=============================================================================
//		Serial1 RX Check 												   
//=============================================================================
BYTE RS1_ready(void)
{
	if( RS1_in == RS1_out ) return 0;
	else return 1;
}
//=============================================================================
//		Serial1 RX														   
//=============================================================================
BYTE RS1_rx(void)
{
	BYTE	ret;
		
	ES1 = 0;
	ret = RS1_buf[RS1_out];
	RS1_out++;
	if(RS1_out >= RS_BUF_MAX) 
		RS1_out = 0;
	ES1 = 1;

	return ret;
}


//=============================================================================
//		Serial1 TX														   
//=============================================================================
void RS1_tx(BYTE tx_buf)
{
	while(RS1_Xbusy);

	ES1 = 0;			// To protect interrupt between SBUF and RS_Xbusy when it's slow
	SBUF1 = tx_buf;
	RS1_Xbusy=1;
	ES1 = 1;
}
#endif

#ifdef DP80390
void delay(WORD cnt)	   // now delay is based on 1ms
{
	BYTE temp;

	cnt <<= 1;
	while (cnt--)
	{
		temp = TL0;
		while (temp == TL0)
		{
			;
		}
	}
}
#else
void delay(WORD cnt)	   // now delay is based on 1ms
{
	/*DWORD ttic01;
	
	ttic01 = tic01;
	if ( cnt == 0 ) return;
	else if ( cnt == 1 ) {
		while ( ttic01 == tic01 ) {;}
		return;
	}
	ttic01 = (tic01 + cnt) % 30000;
	while( tic01 != ttic01 ) {;}
	*/

	
	tic_pc = 0;   //erek 2011-11-10
	do {
			;
	} while(tic_pc < cnt);
}
#endif

//=============================================================================
//                            Watchdog                                                   
//=============================================================================
void EnableWatchdog(void)
{
	EWDI = 0;		// Disable WDT Interrupt
	CKCON |= 0xc0;	// WDT clock = 2^26

	TA = 0xaa;
	TA = 0x55;
	WDCON = 0x03;	// - - - - WDIF WTRF EWT RWT

	EWDI = 1;		// Enable WDT Interrupt (disable for test)
}

void DisableWatchdog(void)
{
	TA = 0xaa;
	TA = 0x55;
	WDCON = 0x00;	// - - - - WDIF WTRF EWT RWT

	EWDI = 0;		// Disable WDT Interrupt
}
/*
void RestartWatchdog(void)
{
	TA = 0xaa;
	TA = 0x55;
	WDCON = 0x03;	// - - - - WDIF WTRF EWT RWT
}
*/

void InitSPIIO()
{
	WriteTW88(0xff, MCU_PAGE);
	WriteTW88(0x03, 0x41);		
	WriteTW88(0x0a, 0x06);	//

	WriteTW88(0x04, 0x01);
	
	WriteTW88(0xff, MCU_PAGE);
	WriteTW88(0x03, 0x42);		
	WriteTW88(0x0a, 0x01);	//
	WriteTW88(0x0b, 0x40);	
	WriteTW88(0x04, 0x01);	
	
	WriteTW88(0xff, MCU_PAGE);
	WriteTW88(0x03, 0x41);		
	WriteTW88(0x0a, 0x04);	//
	WriteTW88(0x04, 0x01);
}


//=============================================================================
//                            Initialize CPU                                                   
//=============================================================================
#ifdef DP80390
void InitCPU(void)
{
	TWBASE = 0x00;//REG_START_ADDRESS/0x100;		// Base address of TW8823

//	CKCON = 0x19;			// default is 0x01, Timer 0, 1 both divide by 12, xmemory timing slowest
	CKCON = 0x18;			// default is 0x01, Timer 0, 1 both divide by 12, xmemory timing slowest
							// xxx1 1001 : timer2 divide by 4, timer1 divide by 4, timer0 divide by 12

	WriteTW88(0xff, MCU_PAGE);

	//WriteTW88(0x22, 0x34);		//0x69);		// Timer0 Divider for tic 0 change it to 0.5msec
	//WriteTW88(0x23, 0xbc);		//0x78);		//
	//WriteTW88(0x22, 0x69);		// Timer0 Divider for tic 0 change it to 1msec
	//WriteTW88(0x23, 0x78);		// 27000
	WriteTW88(0x22, 0x01);		// Timer0 Divider for tic 0 change it to 1msec
	WriteTW88(0x23, 0x0e);		// 270
	//WriteTW88(0x22, 0x0a);		// Timer0 Divider for tic 0 change it to 0.1msec
	//WriteTW88(0x23, 0x8c);		//

	//WriteTW88(0x24, 0x0a);		// Timer1 Divider for 100usec
	//WriteTW88(0x25, 0x8c);		// 	
	WriteTW88(0x24, 0x01);		// Timer1 Divider for 10usec
	WriteTW88(0x25, 0x0e);		// 	

	WriteTW88(0x28, 0);		// baudrate	  for UART0
	WriteTW88(0x29, 12);	// 115200bps = 12	

	WriteTW88(0x2a, 0);		// baudrate	 for UART1
	WriteTW88(0x2b, 12);	// 115200bps = 12	

	/*----- Initialize interrupt -------------*/

	TH1 = 0xff; 			// if input is 27MHz 9600 bps, using outside devider 

	SCON = 0x50;			// 0101 0000 mode 1 - 8 bit UART				
							// Enable serial reception						
	SCON1 = 0x50;			// 0101 0000 mode 1 - 8 bit UART				
							// Enable serial reception						
    TMOD = 0x66;			// 0110 0110 timer 0 - 8 bit auto reload		
							// timer1/timer0 use counter mode
							// timer 1 - baud rate generator				
    TCON = 0x55;			// 0101 0001 timer 0,1 run						
							// int 0,  edge triggered						
							// int 1,  edge triggered						
							// TF1 TR1 TF0 TR0	EI1 IT1 EI0 IT0				

//	TH0 = 131;				// count 256 - 125 = 131
	TH0 = 156;//makes 1ms interrupt	// count 256 - 200 = 56: makes it 0.2sec interrupt

	TH1 = 156;				// makes 1ms  for TOUCH SAR sensing timer

	//T2CON  = 0x11;                  // Timer 2 fdiv/12, reload enabled            
	T2CON  = 0x12;                  // Timer 2 Counter mode with 27MHz/divider

	//RCAP2H = 0xe7;
	//RCAP2L = 0x96;
	RCAP2H = 0x96;			// 0x9688 = 0x10000 - 27000
	RCAP2H = 0x88;			// 0x9688 = 0x10000 - 27000
	                
	TH2 = RCAP2H;
	TL2 = RCAP2L;
	ET2 = 0;                                 // Disable Timer 2

	PCON = PCON | 0xc0;		// 1100 0000 SMOD0(double baud rate bit) = 1,	SMOD1(double baud rate bit) = 1		
	//IP	 = 0x02;			// 0000 0000 interrupt priority					
	IP	 = 0x00;			// 0000 0000 interrupt priority					
							// -  - PT2 PS PT1 PX1 PT0 PX0	 		         

	UART0FIFO = 0x80;		// make UART0 FIFO on
	UART1FIFO = 0x80;		// make UART1 FIFO on

//	IE	 = 0xB2;			// 1101 0010 interrupt enable:Serial0,1,TM0		
  							// EA ES1 ET2 ES ET1 EX1 ET0 EX0					
	ET0  = 1;
	ET1  = 0;
	ES   = 1;
	ES1  = 1;

	TI	 = 1;				// For Starting Serial TX 
	TI1	 = 1;				// For Starting Serial TX 

//	EINT6 = 1;				// enable touch interrupts

//	EIE = 0x0A;				// 0000 1010 : EX5=1, EX3=1;
//	EIE = 0x02;				// 0000 0010 : EX5=0, EX3=1;	only IRQ
//	EIE = 0x08;				// 0000 1000 : EX5=1, EX3=0;	only Power Down
//	EIE = 0x00;				// disable all interrupts 

	//-----------------------------------------------------------------

	RM_get = 0;		// Remocon receive reset
	//EX0 = 1;		// Enable Remocon (Enable Ext int0)
	EX0 = 0;		// Disable Remocon (Disable Ext int0)
	EX1 = 0;		// Disable ex1


	INTENABLE = 0xff;
	P1 = INTENABLE;
	INTENABLE = 0;
	//P1 = INTENABLE;
	EA   = 1;

	//------------ GPIO for LED ---------------------------------------
//	P1_3=0;
//	P1_4=0;
	#ifdef TW8823SHDEMO
//	P1_2=0;
//	P3_5=D115_ON;
	#endif
}
#else
void InitCPU(void)
{
	TWBASE = 0x00;//REG_START_ADDRESS/0x100;		// Base address of TW8823

//	CKCON = 0x19;			// default is 0x01, Timer 0, 1 both divide by 12, xmemory timing slowest
	CKCON = 0x18;			// default is 0x01, Timer 0, 1 both divide by 12, xmemory timing slowest
							// xxx1 1001 : timer2 divide by 4, timer1 divide by 4, timer0 divide by 12

	WriteTW88(0xff, MCU_PAGE);
//	WriteTW88(0x22, 1);		// Timer0 Divider for tic 0.
//	WriteTW88(0x23, 176);	//
	//WriteTW88(0x22, 1);		// Timer0 Divider for tic 0.
	//WriteTW88(0x23, 14);	//

	WriteTW88(0x24, 0);		// Timer1 Divider for baudrate
	WriteTW88(0x25, 12);	// 115200bps = 12	

	WriteTW88(0x28, 0);		// baudrate	  for UART0
	WriteTW88(0x29, 12);	// 115200bps = 12	

	WriteTW88(0x2a, 0);		// baudrate	 for UART1
	WriteTW88(0x2b, 12);	// 115200bps = 12	

	/*----- Initialize interrupt -------------*/

	TH1 = 0xff; 			// if input is 27MHz 9600 bps, using outside devider 

	SCON = 0x50;			// 0101 0000 mode 1 - 8 bit UART				
							// Enable serial reception						
	SCON1 = 0x50;			// 0101 0000 mode 1 - 8 bit UART				
							// Enable serial reception						
    TMOD = 0x66;			// 0010 0010 timer 0 - 8 bit auto reload		
							// timer1 use interal 27MHz, timer0 use external
							// timer 1 - baud rate generator				
    TCON = 0x55;			// 0101 0001 timer 0,1 run						
							// int 0,  edge triggered						
							// int 1,  edge triggered						
							// TF1 TR1 TF0 TR0	EI1 IT1 EI0 IT0				

//	TH0 = 131;				// count 256 - 125 = 131
	//TH0 = 156;				// count 256 - 100 = 156
	TH0 = 254;				//it makes 1ms interrupt. system clock/RG_DVIDT0/(256-TH0)  erek 2011-11-10

	PCON = PCON | 0xc0;		// 1100 0000 SMOD0(double baud rate bit) = 1,	SMOD1(double baud rate bit) = 1		
	//IP	 = 0x02;			// 0000 0000 interrupt priority					
							// -  - PT2 PS PT1 PX1 PT0 PX0	 	
	IP=0x10;

//	IE	 = 0xB2;			// 1101 0010 interrupt enable:Serial0,1,TM0		
  							// EA ES1 ET2 ES ET1 EX1 ET0 EX0					
	EA   = 1;
	ET0  = 1;
	ES   = 1;
	ES1  = 1;
	ES   = 1;				// 

	TI	 = 1;				// For Starting Serial TX 
	TI1	 = 1;				// For Starting Serial TX 

//	EIE = 0x0A;				// 0000 1010 : EX5=1, EX3=1;
//	EIE = 0x02;				// 0000 0010 : EX5=0, EX3=1;	only IRQ
//	EIE = 0x08;				// 0000 1000 : EX5=1, EX3=0;	only Power Down
//	EIE = 0x00;				// disable all interrupts 

	//------------ Timer 2 for Remocon --------------------------------
	T2CON  = 0x02;		// Timer2 Clear with counter
	RCAP2L =0;
	RCAP2H=0;
	TR2	   = 1;
//	ET2    = 1;
//	C_T2   = 1;			// timer source is external T2
//	InitForRemo();
	//-----------------------------------------------------------------

	//EX0 = 1;		// Enable Remocon (Enable Ext int0)
	EX0 = 0;		// Disable Remocon (Disable Ext int0)


	INTENABLE = 0xff;
	P1 = INTENABLE;
	INTENABLE = 0;
	//P1 = INTENABLE;

	//------------ GPIO for LED ---------------------------------------
//	P1_3=0;
//	P1_4=0;
	#ifdef TW8823SHDEMO
//	P1_2=0;
//	P3_5=D115_ON;
	#endif
}

#endif

//Timer0 App
BOOL WaitTimer0(BYTE TaskID)
{
	BOOL bool = (BOOL)((Timer0TaskDly[TaskID] == 0)&&(((Timer0TaskAct>>TaskID) & 0x01) != 0)&&(TaskID < MAXTIMER0_TASK));
	return (bool);
}

BYTE AddTimer0_Dly(BYTE Timeout)
{
	BYTE TaskID=0;

	for(TaskID=0; TaskID<MAXTIMER0_TASK; TaskID++){
		if(((Timer0TaskAct>>TaskID) & 0x01) == 0){
			Timer0TaskDly[TaskID] = Timeout;
			if(Timeout < 0xff){
				Timer0TaskAct |= (0x01<<TaskID);
			}
			return TaskID;
		}
	}

	return MAXTIMER0_TASK;
}

/*
void DelTimer0_Dly(BYTE TaskID)
{
	if(TaskID >= MAXTIMER0_TASK){
		return;
	}
	Timer0TaskDly[TaskID] = 0;
	Timer0TaskAct &= ~(0x01<<TaskID);
	return;
}
*/

void RstTimer0_Dly(BYTE TaskID, BYTE Timeout)
{
	if (TaskID >= MAXTIMER0_TASK)
	{
		return;
	}
	Timer0TaskDly[TaskID] = Timeout;
	if (Timeout < 0xff)
	{
		Timer0TaskAct |= (0x01<<TaskID);
	}
	else
	{
		Timer0TaskAct &= ~(0x01<<TaskID);
	}

	return;
}






